/**
 * Widok dla analogowego wskaźnika
 */
package com.beans.indicator.view;

import com.beans.indicator.constans.IndicatorConstans;
import com.beans.indicator.model.IndicatorModel;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.Observable;

/**
 *
 * @author Wojtek
 */
public class AnalogIndicatorView extends AbstractIndicatorView {

    //średnica tarczy wskaźnika
    private int size;
    //współrzędne środka tarczy
    private int centerX, centerY;
    //odstęp pomiędzy tarczą a granicą komponentu
    private int spacing = 5;
    //punkt w którym ma zacząć malować podziałkę
    //0.25 PI czyli startujemy na godzinie 4:30 (0 na godzinie 6:00)
    private final static float offset = (float)(1/4.0 * Math.PI);

    public AnalogIndicatorView(IndicatorModel model) {
        super(model);
        //dodajemy listener na zmianę rozmiaru komponentu
        this.addComponentListener(new ComponentListener() {

            public void componentResized(ComponentEvent e) {
                int w = getWidth();
		int h = getHeight();
		size = ((w < h) ? w : h) - 2 * spacing;
		centerX = size / 2 + spacing;
		centerY = size / 2 + spacing;
            }

            public void componentMoved(ComponentEvent e) {
            }

            public void componentShown(ComponentEvent e) {
            }

            public void componentHidden(ComponentEvent e) {
            }
        });
    }


    @Override
    protected void paintIndicatorFace(Graphics g) {
        Graphics2D g2 = getAAInstance(g);
        g2.setColor(background);
	g2.fillOval(spacing, spacing, size, size);
	g2.setColor(Color.black);
	g2.drawOval(spacing, spacing, size, size);
	for (int sec = 0; sec <= tickCount; sec++) {
            int ticStart;
            if (sec % largeTickInterval == 0) {
                ticStart = size / 2 - 10;
            } else {
                ticStart = size / 2 - 5;
            }
            drawRadius(g2, centerX, centerY, offset + getRadsPerTick() * sec, ticStart, size / 2);
	}
    }
    @Override
    protected void paintDiode(Graphics g,Color c){
        Graphics2D g2 = getAAInstance(g);
        g2.setColor(c);
        g2.fillOval(centerX-2, centerY+10, size/10, size/10);
    }
    
    @Override
    protected void paintTitle(Graphics g, String label){
        Graphics2D g2 = getAAInstance(g);
        g2.setColor(Color.BLACK);
        g2.drawString(label, size/2,size/2 - size/10);
    }

    @Override
    protected void paintIndicator(Graphics g) {
        Graphics2D g2 = getAAInstance(g);
        g2.setColor(Color.BLACK);
        drawRadius(g2, centerX, centerY, offset+(1.5*Math.PI*(model.getMaxValue()-model.getValue()))/model.getMaxValue(), 0, size/2);
    }

    private void drawRadius(Graphics g, int x, int y, double angle, int minRadius, int maxRadius) {
    	float sin = (float) Math.sin(angle);
        float cos = (float) Math.cos(angle);
	int dxmin = (int) (minRadius * sin);
	int dymin = (int) (minRadius * cos);
	int dxmax = (int) (maxRadius * sin);
	int dymax = (int) (maxRadius * cos);
	g.drawLine(x + dxmin, y + dymin, x + dxmax, y + dymax);
    }

    private float getRadsPerTick() {
        return (float) (1.5*Math.PI/tickCount);
    }

}
